package com.wt.springsamples;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.wt.springsamples.model.dto.UserLoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class TestRestService {
    @Autowired
    private RestTemplate restTemplate;

    public static void main(String[] args) {
        String s ="{\n" +
                "    \"get\": {\n" +
                "        \"type\": \"jobs\",\n" +
                "        \"ids\": [\n" +
                "            7\n" +
                "        ],\n" +
                "        \"mode\": \"output\",\n" +
                "        \"block_ids\": [\n" +
                "            0\n" +
                "        ],\n" +
                "        \"task_ids\": [\n" +
                "\n" +
                "        ],\n" +
                "        \"mon_id\": 1\n" +
                "    }\n" +
                "}";
        System.out.println(s.length());

    }

    public ResponseEntity sendRequestJson(){
        String requestUrl = "http://localhost:8080/file/receiveRequest";
        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        //1.设置请求类型 上传文件必须用表单类型
        headers.setContentType(MediaType.APPLICATION_JSON);
        UserLoginInfo info  = new UserLoginInfo();
        info.setName("wangtao");
        info.setEmail("scwt@sina.cn");
        info.setUserId(2222L);
        HttpEntity httpEntity = new HttpEntity<>(JSON.toJSONString(info, SerializerFeature.WriteMapNullValue), headers);
        //6.发起请求
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(requestUrl, httpEntity, String.class);

        return responseEntity;
    }
}
